<style scoped>
    .layui-row {
        overflow: hidden;
    }
    .select-item {
        width: 34.333333%;
        float: right;
    }
    .layui-row:after, .layui-row:before {
        clear: none;
    }
    .layui-row {
        overflow: hidden;
    }
    .select-item {
        width: 34.333333%;
        float: right;
    }
    .layui-row:after, .layui-row:before {
        clear: none;
    }
</style>

<template>
    <div class="salesSpecification">
        <div>
            <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加批次</a>
        </div>

        <form class="layui-form " >
            <div  class="layui-col-lg4 select-item" style="text-align: right;width: auto" >

<!--                <div class="layui-input-inline" style="width: 190px">-->
<!--                    <input type="text" ref="bcode" placeholder="请输入批次编号" class="layui-input key">-->
<!--                </div>-->
                <div class="layui-inline" style="width: 160px" lay-filter="batch">
                    <select ref="batch" class="layui-form-select">
                        <option value="">请选择批次</option>
                        <option v-for="(item, index) in batches" :value="item.code">{{item.name}}</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 190px">
                    <input type="text" ref="bname" placeholder="请输入批次名称" class="layui-input key">
                </div>
                <div class="layui-inline" lay-filter="batchmainid" style="width: 160px">
                    <select ref="batchmainid" class="layui-form-select">
                        <option value="">请选择采收主体</option>
                        <option v-for="(item, index) in companys" :value="item.guid">{{item.name}}</option>
                    </select>
                </div>
                <div class="layui-inline" lay-filter="status" style="width: 160px">
                    <select ref="status" class="layui-form-select">
                        <option value="">请选择状态</option>
                        <option value="1">进行中</option>
                        <option value="2">已结束</option>
                    </select>
                </div>
                <button type="button" class="layui-btn sou" @click="search">搜索</button>
            </div>

        </form>

        <table class="layui-table layui-form">

            <thead>
            <tr>
                <th>编号</th>
                <th>名称</th>
                <th>采收主体</th>
                <th>部门</th>
                <th>品种</th>
                <th>数量</th>
                <th>质检方式</th>
                <th>创建人</th>
                <th>创建时间</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                <tr v-for="(val, index) in listdata">
                    <td>{{val.code}}</td>
                    <td>{{val.name}}</td>
                    <td>{{val.batchmain}}</td>
                    <td>{{val.departname}}</td>
                    <td>{{val.variety}}</td>
                    <td>{{val.plannumber}}</td>
                    <td>{{val.mode == 1 ? '库房质检' : '地头质检'}}</td>
                    <td>{{val.operator}}</td>
                    <td>{{val.createdate}}</td>
                    <td>{{val.status == 1 ? '进行中' : '已结束'}}</td>
                    <td><a href="javascript:;" @click="edit(val.guid)">修改</a></td>
                </tr>
            </tbody>
        </table>

        <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage" ></Pagination>
        <div style="height: 200px"></div>
    </div>
</template>

<script>
    import Pagination from '@/components/common/Pagination.vue'
    export default {
        name: 'BatchList',
        components: {Pagination},
        data () {
            return {
                listdata: [],
                currentPage: 1,
                totalPage: 0,
                companys: [],
                batches: []
            }
        },
        mounted() {
            let that = this;
            that.findCodeForSelect();
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;
                    that.getPageData();
                    that.getBatches();
                    that.loadOrganization();
            });

        },
        updated () {
            layui.form.render("select");
        },
        methods: {
            findCodeForSelect: function(){
                this.axios({
                    method: 'get',
                    url: '/Controller/harvest_Procurementbatch!findCodeForSelect.action'
                }).then((response) => {
                    let res = response.data;
                    console.log(res)
                })
            },
            search: function() {
                this.getPageData('search');
            },
            turnPage: function(data){
                this.currentPage = data;
                this.getPageData();
            },
            getPageData: function(type = '') {
                let that = this;
                let loader = layui.layer.load();

                let postData = {};
                if (type != 'search') {
                    postData.page = that.currentPage;
                }
                postData.code = this.$refs.batch.value;
                postData.search = this.$refs.bname.value;
                postData.orgguid = this.$refs.batchmainid.value;
                postData.status = this.$refs.status.value;
                postData.functiontype = 15;

                this.axios({
                    method: 'post',
                    url: '/Controller/harvest_Procurementbatch!list.action',
                    params: postData
                }).then((response) => {
                    layer.close(loader);

                    let res = response.data;
                    if (res.info != undefined && res.info.rows != undefined) {
                        that.currentPage = res.info.indexPage;
                        that.totalPage = res.info.pageCount;

                        that.listdata = res.info.rows;
                    } else {
                        this.toastError("获批次格数据失败");
                    }
                })
            },
            getBatches: function () {
                let that = this;
                this.axios({
                    method: 'post',
                    url: '/harvest/Procurementbatch!findBatchbyFun.action',
                    params: {status: 1, functiontype: 15}
                }).then((response) => {
                    if (response.data.batchs != null ) {
                        this.batches = response.data.batchs;
                        layui.form.render("select");
                    } else {
                        that.toast("获取批次数据失败");
                    }
                })
            },
            loadOrganization: function () {
                this.axios({
                    method: 'post',
                    url: '/sales/Organization!listAll.action'
                }).then((response) => {
                    let res = response.data;
                    if (res.info != undefined && res.info != 'error' ) {
                        this.companys = res.info;
                        layui.form.render("select", "batchmainid");
                    } else {
                        that.toast("系统错误");
                    }
                })
            },
            add: function () {
                this.$parent.switchPage('addBatch');
            },
            edit: function (id) {
                this.$parent.gid = id;
                this.$parent.switchPage('editBatch');
            },
            del: function (val) {
                let that = this;
                layer.confirm('确定要禁用该数据', function () {
                    let loader = layer.load();
                    $.ajax({
                        url: '/Controller/harvest_Procurementbatch!delete.action',
                        type: "post",
                        data: val,
                        dataType: "json",
                        success: function(res) {
                            if (res.msg == "success") {
                                that.toast("删除成功");
                                that.getPageData('search');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                })
            },
        }
    }
</script>
